local U = require "packages.utility.utility"

local zhiwen = fk.CreateSkill{
  name = "xiaobai__zhiwen"
}

zhiwen:addEffect("viewas",{
  anim_type = "offensive",
  pattern = "slash|duel",
  prompt = "#xiaobai__zhiwen",
  interaction = function(self, player)
    local all_names = {"slash", "duel"}
    local names = player:getViewAsCardNames( zhiwen.name, all_names)
    if  player:getMark("@@xiaobai__zhiwen-onlyslash")>0 then
      table.removeOne(names, "duel")
    end
    if player:getMark("@@xiaobai__zhiwen-onlyduel") >0 then
      table.removeOne(names, "slash")
    end
    if #names > 0 then
      return UI.CardNameBox { choices = names, all_choices = all_names }
    end
  end,
  card_filter = function(self, player, to_select, selected)
    if #selected > 3 then return false end
    
    local handcards = player:getCardIds("h")
    if #selected==0 and #handcards == 1 and  player:getMark("@@xiaobai__zhiwen-three")==0 then
      return true
    end
    
    return #selected<3 and player:getMark("@@xiaobai__zhiwen-one")==0
  end,
  view_as = function(self, player, cards)
    if  not self.interaction.data then return end
    local n=#player:getCardIds("h")
    if not (#cards==1 and n==1 or #cards==3)then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = zhiwen.name
    card:addSubcards(cards)
    return card
  end,
  before_use = function (self, player, use)
    local room = player.room
    local choices={"zhiwen_type","zhiwen_name","zhiwen_beishui"}
    local choice = player.room:askToChoice(player, {
      skill_name = zhiwen.name,
      choices = choices,
      prompt = "#xiaobai__zhiwen-invoke"
    })
    if choice=="zhiwen_beishui" then
        use.card.extra_data= use.card.extra_data or {}
        table.insert(use.card.extra_data, "shifen_beishui")

      
    end
    local marks={"@@xiaobai__zhiwen-onlyslash","@@xiaobai__zhiwen-onlyduel","@@xiaobai__zhiwen-three","@@xiaobai__zhiwen-one"}
    for _,mark in ipairs(marks) do
      room:setPlayerMark(player, mark, 0)
    end
    if choice~="zhiwen_type" then
        if use.card.name=="slash" then
            room:setPlayerMark(player, "@@xiaobai__zhiwen-onlyduel", 1)
        elseif use.card.name=="duel" then
            room:setPlayerMark(player, "@@xiaobai__zhiwen-onlyslash", 1)
        end
    end
    if choice~="zhiwen_name" then
        local n=#use.card.subcards
        if #use.card.subcards==1 then
          room:setPlayerMark(player, "@@xiaobai__zhiwen-three", 1)
        elseif #use.card.subcards==3 then
          room:setPlayerMark(player, "@@xiaobai__zhiwen-one", 1)
        end
    end
  end,
  enabled_at_play = Util.TrueFunc;
  enabled_at_response = Util.FalseFunc;
})
zhiwen:addEffect("targetmod",{
  frequency = Skill.Compulsory,
  bypass_times = function(self, player, skill, scope, card)
    return card and table.contains(card.skillNames, zhiwen.name)    
  end,
})

zhiwen:addEffect(fk.DamageCaused,{
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player  and data.card and data.card.extra_data and table.contains(data.card.extra_data, "shifen_beishui") and data.to~=player
  end,
  on_cost = function(self, event, target, player, data)
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke("xiaobai__zhiwen")
    room:notifySkillInvoked(player, "xiaobai__zhiwen")
    data.damage = data.damage + 1
  end,
})
Fk:loadTranslationTable{
  ["xiaobai__zhiwen"] = "鸷刎",
  [":xiaobai__zhiwen"] = [[你可以将三张牌或最后一张手牌当作无次数限制的【杀】或【决斗】使用，然后你选择一项：
  1.下次仅能以另一种方式转化；2.下次仅能转化为另一种牌名。背水：此牌对其他角色造成的伤害+1。"]],
  ["zhiwen_type"] = "下次仅能以另一种方式转化",
  ["zhiwen_name"] = "下次仅能转化为另一种牌名",
  ["zhiwen_beishui"] = "背水：此牌造成的伤害+1",
  ["@@xiaobai__zhiwen-onlyslash"] = "仅能转化杀",
  ["@@xiaobai__zhiwen-onlyduel"] = "仅能转化决斗",
  ["@@xiaobai__zhiwen-three"] = "仅能转化三张牌",
  ["@@xiaobai__zhiwen-one"] = "仅能转化最后一张牌",
  ["#xiaobai__zhiwen"] = "鸷刎：你可以将三张牌或最后一张手牌当作无次数限制的【杀】或【决斗】使用。",
  ["#xiaobai__zhiwen_targetmod"] = "鸷刎",
  ["#xiaobai__zhiwen-invoke"] = "鸷刎：请选择！",

  ["$xiaobai__zhiwen2"] = "君非梁鸿，然妾心三冬无改、万苦难移。",
  ["$xiaobai__zhiwen1"] = "心非草木，今名登鬼录，必衔怨以报！",
}

return zhiwen